package club.justwill.single.persistence.store;

import club.justwill.single.persistence.document.UserFollowProject;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Component;

/**
 * Created by luoyu on 1/18/16.
 */

@Component
@Scope(ConfigurableBeanFactory.SCOPE_SINGLETON)
public class UserFollowProjectStore extends MongoDbStore<UserFollowProject> {
    public UserFollowProject  searchFollowedProject(long userId) {
        return mongoTemplate.findOne(Query.query(Criteria.where("user_id").is(userId)),UserFollowProject.class);
    }

    public void updateFollowedProject(long userId, long projectId) {
        UserFollowProject project = searchFollowedProject(userId);
        if(project.getFollowedProjectIdList().contains(projectId)){
            project.getFollowedProjectIdList().remove(projectId);
            save(project);
        }
    }
}
